package com.zhenwei.zscm.service;

import com.zhenwei.zscm.common.base.RestResult;
import com.zhenwei.zscm.common.base.UserContext;
import com.zhenwei.zscm.model.dto.log.LogDTO;
import com.zhenwei.zscm.model.vo.log.QueryExLogVO;
import com.zhenwei.zscm.model.vo.log.QueryLogVO;
import com.zhenwei.zscm.repo.ExceptionLogRepo;
import com.zhenwei.zscm.repo.LogRepo;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@AllArgsConstructor
public class LogService {

    private LogRepo logRepo;
    private ExceptionLogRepo exceptionLogRepo;

    public RestResult list(QueryLogVO query) {
        var list = logRepo.list(query);
        return RestResult.success(list, query.getTotalCount());
    }
    public RestResult getExceptionLogs(QueryExLogVO query) {
        if (UserContext.isNotAdmin()) {
            return RestResult.success(new ArrayList<>(), 0);
        }
        var list = exceptionLogRepo.list(query);
        return RestResult.success(list, query.getTotalCount());
    }

    public RestResult getUserLoginLogs(Integer pageNo,Integer pageSize){
        List<LogDTO> list = logRepo.listByType("LOGIN", pageNo, pageSize);
        return RestResult.success(list, list.size());
    }

    public RestResult getExLogTypes(){
        var list = exceptionLogRepo.getLogTypes();
        return RestResult.success(list, list.size());
    }

    public RestResult getLogTypes(){
        var list = logRepo.getLogTypes();
        return RestResult.success(list, list.size());
    }

}
